/*
 * ShellCommand.h
 *
 *  Created on:  May 29, 2009
 *      Author:  Zach Riggle A37627537
 *      Project: CSE410 Project 1
 */

#ifndef SHELLCOMMAND_H_
#define SHELLCOMMAND_H_

#include <unistd.h>
#include <vector>
#include <string>
#include <iostream>
#include <list>
#include <regex.h>
#include "Shell.h"
#include "ChildProcess.h"
#include "RegularExpression.h"
using namespace std;

// pthread function.
void *backgroundShellCommand(void*);

class ShellCommand : public ChildProcess {
public:
    static const string hist;
    static const string cd;
    static const string curr;
    static const string curPID;
    static const string quit;
    static const string amp;
    static const string pipe;
    static const string redirStdout;
    static const string redirStdin;
    static const int maxCommandLength;

    ShellCommand(string command, Shell *parent);
    virtual ~ShellCommand();
    static bool isWhitespace(string s);
    bool checkBackground();
    void run();
    void background();
    const string getCommand();
    bool isBackground();
    void setBackgroundNumber(int newNum);
    int getBackgroundNumber();
    static const string commandDelimiters;
    static const string whitespace;

    // Regular expressions...
    static RegularExpression *validCommandExpression_;
    static RegularExpression *backgroundExpression_;

protected:
    bool validateCommand();
    void execute();
    int backgroundNumber;
    void executeCommand(const string command, const list<string> arguments,
                        istream input, ostream output);
    void parseCommand();
    string command;
    bool runInBackground;
    bool parseError;
    vector<string> commands;
    vector<string> delimiters;
    Shell *parent;
    int returnValue;
};

#endif /* SHELLCOMMAND_H_ */
